package com.javaclimb.accounting.service.impl;

import com.github.pagehelper.PageHelper;
import com.javaclimb.accounting.dao.FlowDao;
import com.javaclimb.accounting.po.FlowBack;
import com.javaclimb.accounting.service.FlowService;
import com.javaclimb.accounting.vo.CardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FlowServiceImpl implements FlowService{
    @Autowired
    private FlowDao flowDao;
    //得到所有流水
    @Override
    public List<FlowBack> getAllFlow(long userAccId, String userName,String type, Date startTime, Date endTime, String key,
                                     String userReal, int pageNum, int pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        return flowDao.getAllFlow(userAccId,userName,type,startTime,endTime,key,userReal,pageNum,pageSize);
    }


    @Override
    //得到总条数
    public int getTotalNumber(long userAccId,String userName, String type, Date startTime, Date endTime, String key,
                              String userReal, int pageNum, int pageSize) {
        return flowDao.getTotalNumber(userAccId,userName,type,startTime,endTime,key,userReal,pageNum,pageSize);
    }

    @Override
    public CardVo getCardById(long cardId) {
        return flowDao.getCardById(cardId);
    }



    //对时间进行操作
    public Date changeTime(Date date,int time){
        // 获取当前时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // 把日期往后增加一天,整数  往后推,负数往前移动
        calendar.add(Calendar.DATE, time);
        // 这个时间就是日期往后推一天的结果
        date = calendar.getTime();
        return date;
    }

    @Override
    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public int checkTime(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2) //不同年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0) //闰年
                {
                    timeDistance += 366;
                }
                else //不是闰年
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2-day1) ;
        }
        else //同一年
        {
            return day2-day1;
        }
    }




}
